package br.com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.connection.ConnectionFactory;
import br.com.controller.NivelAcessoController;

@WebServlet("/NivelAcessoServlet")
public class NivelAcessoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public NivelAcessoServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		try{
			doNacesso(request, response);
		}catch (Exception e){
			out.println("ocorreu algum erro");
		}

	}

	protected void doNacesso(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		String descricao = request.getParameter("descricao");

		Connection conn = null;

		try {

			conn = new ConnectionFactory().getConnection();
			NivelAcessoController.inserir(conn, descricao);
			out.print("<script type=\"text/javascript\">alert(\"Cadastrado com sucesso.\");</script>");			
			out.print("<meta http-equiv='refresh' ");
			conn.commit();

		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (Exception ex) {ex.printStackTrace();}
		} finally {
			try {
				conn.close();
			} catch (Exception ex) {}
		} 
	}

}
